# encoding=utf-8
from LBT.BaseParent.BaseLbt import lbtlogin
from LBT.Resource.StudentAttendance.StudentAttendanceAtt import StudentAttendanceAtt
from LBT.GeneralOperation.StudentOperation import StudentOperation


class ClassAttendance(lbtlogin, StudentAttendanceAtt):
    '''学生考勤统计及明细模块
    1、按天统计--根据数据库查询最后一个出勤学生，及当天出勤次数，筛选核对
    2、按月查询--根据数据库查询当月第一个有出勤的学生，及次数，筛选核对
    '''

    @lbtlogin.loginFun
    def test_checkLastAtt(self):
        """
        根据数据库查询最后一个出勤学生，及当天出勤次数，筛选核对
        :return:
        """
        self.docstr = '按天统计--根据数据库查询最后一个出勤学生，及当天出勤次数，筛选核对'
        dr = self.driver
        self.moduleOp.openMoudleAndIn(self.studentatten_navigation_class, self.studentatten_detail_url,
                                      self.student_topnavigation_class)
        # 查询数据库最后一次刷卡人的班级、时间、人名
        sql = "select top 1 ed.eduunitname,u.name,convert(varchar(10),att.cardtime,121)  from attendance att inner join users u on u.usersid = att.studentid inner join student st on st.studentid = u.usersid inner join eduunit ed on ed.eduunitid = st.eduunitid where u.schoolid = (select schoolid from school where schoolname ='" + self.lbt_schoolname_text + "') and u.state =1 and ed.state =1 order by att.cardtime desc"
        infolist = self.selectlbtmutilcolumn(sql)
        classname = str(infolist[0][0])
        stuname = str(infolist[0][1])
        time = str(infolist[0][2])
        # 根据首条结果，查询页面和数据库进行核对
        stuopr = StudentOperation(dr)
        stuopr.selectWithInput(dr, 0, 0, classname)
        stuopr.selectWithInput(dr, 1, 1, stuname)
        stuopr.selectWithOutInput(dr, 2, '已出勤')
        selecttime = time + " 至 " + time
        self.clearAndSendKeyForText(dr.find_element_by_id(self.classatt_selecttime_id), selecttime)
        dr.find_element_by_css_selector(self.classatt_selectbtn_css).click()
        results = dr.find_elements_by_xpath(self.classatt_results_xpath)
        self.assertEqual(results.__len__(), 1)
        resultinfo = {}
        resultinfo["stuname"] = str(results[0].text.split(" ")[3])
        resultinfo["datestr"] = str(results[0].text).split(" ")[0]
        resultinfo["classname"] = str(results[0].text.split(" ")[2])
        self.assertEqual(stuname, resultinfo["stuname"])
        self.assertIn(time, resultinfo["datestr"])
        self.assertIn(classname, resultinfo["classname"])
        # 导出刚才查询的那一条，然后和数据库核对
        self.deleteAllFiles(self.defaultdownpath)
        dr.find_element_by_css_selector(self.classatt_exportbtn_css).click()
        downlist = self.readExcle(self.defaultdownpath)
        self.assertEqual(stuname, str(downlist[1][2]))
        self.assertIn(str(time), str(downlist[1][0]))
        self.assertIn(str(classname), str(downlist[1][1]))

    @lbtlogin.loginFun
    def test_checkStatisticsAtt(self):
        """
        按月查询--根据数据库查询当月第一个有出勤的学生，及次数，筛选核对
        :return:
        """
        self.docstr = '按月查询--根据数据库查询当月第一个有出勤的学生，及次数，筛选核对'
        dr = self.driver
        self.moduleOp.openMoudleAndIn(self.studentatten_navigation_class, self.studentatten_detail_url,
                                      self.student_topnavigation_class)
        # 数据库查询出勤次数最多的学生，及次数、班级、姓名、月份。
        sql = "select top 1 convert(varchar(7),a.cardtime,121),u.name,edu.eduunitname,COUNT(Distinct convert(varchar(10),a.cardtime,121)) As Cnt from attendance a inner join users u on u.usersid = a.studentid inner join student st on st.studentid = u.usersid inner join eduunit edu on edu.eduunitid = st.eduunitid where u.schoolid = (select schoolid from school where schoolname = '" + self.lbt_schoolname_text + "') and u.state =1 and u.usertype =2 group by convert(varchar(7),a.cardtime,121),edu.eduunitname,u.name order by Cnt desc"
        listinfo = self.selectlbtmutilcolumn(sql)
        resultinfo = {}
        resultinfo["count"] = int(listinfo[0][3])
        resultinfo["datestr"] = str(listinfo[0][0])
        resultinfo["classname"] = str(listinfo[0][2])
        resultinfo["name"] = str(listinfo[0][1])
        # 如果存在出勤天数大于1天的，就看一下明细进行核对
        if resultinfo["count"] > 0:
            dr.find_element_by_link_text(self.stuatt_monthpage_text).click()
            dr.find_element_by_id(self.stuatt_month_id).click()
            #dr.find_element_by_id(self.stuatt_month_id).send_keys(resultinfo["datestr"])
            yearinfo = str(resultinfo["datestr"]).split("-")[0]
            dr.find_element_by_class_name(self.stuatt_monthyear_class).click()
            dr.find_element_by_xpath("//li[@lay-ym='"+yearinfo+"']").click()
            monthinfo=str(int(str(resultinfo["datestr"]).split("-")[1])-1)
            dr.find_element_by_xpath("//li[@lay-ym='"+monthinfo+"']").click()
            dr.find_element_by_class_name(self.stuatt_confirm_class).click()
            stuopr = StudentOperation(dr)
            stuopr.selectWithInput(dr, 0, 0, resultinfo["classname"])
            stuopr.selectWithInput(dr, 1, 1, resultinfo["name"])
            dr.find_element_by_id(self.stuatt_selectbtnbymonth_id).click()
            results = dr.find_elements_by_xpath(self.classatt_results_xpath)
            self.assertEqual(results.__len__(), 1)
            resultinfo1 = {}
            resultinfo1["stuname"] = str(results[0].text.split(" ")[1])
            resultinfo1["classname"] = str(results[0].text).split(" ")[0]
            resultinfo1["count"] = str(results[0].text.split(" ")[3])
            self.assertEqual(resultinfo["name"], resultinfo1["stuname"])
            self.assertEqual(resultinfo["count"], int(resultinfo1["count"]))
            self.assertIn(resultinfo["classname"], resultinfo1["classname"])
            self.deleteAllFiles(self.defaultdownpath)
            dr.find_element_by_css_selector(self.classatt_exportbtn_css).click()
            #然后和导出的数据进行比对
            downinfo=self.readExcle(self.defaultdownpath)
            infodown=downinfo[1]
            self.assertEqual(infodown[0],resultinfo["datestr"])
            self.assertEqual(infodown[1], resultinfo1["classname"])
            self.assertEqual(infodown[2],resultinfo1["stuname"])
            self.assertEqual(int(infodown[4]), int(resultinfo1["count"]))
            #然后和明细比对
            dr.find_element_by_css_selector(self.stuatt_detail_css).click()
            stuopr.selectWithOutInput(dr,2,"已出勤")
            dr.find_element_by_css_selector(self.classatt_selectbtn_css).click()
            count=dr.find_element_by_xpath(self.stuatt_detailcount_xpath).text
            self.assertEqual(int(count),int(infodown[4]))
